/**
 * 定义一系列辅助函数
*/


const toString = Object.prototype.toString

// 判断是否是日期类型
export function isDate(val: any): boolean {
  return toString.call(val) === '[object Date]'
}

// 判断是否是对象
export function isObject(val: any): boolean {
  return val !==null && typeof val === 'object'
}

// 判断是否普通对象(let obj = {name: xxx, age: xxx })
export function isPlainObject(val: any): boolean {
  return toString.call(val) === '[object Object]'
}

// 将from对象中的属性和方法拷贝到to对象中
// for in 拷贝包括原型链中的属性和方法
export function extend<T,U>(to: T, from: U): T & U {
  for(let key in from) {
    ;(to as T & U)[key] = from[key] as any
  }
  return to as T & U;
}

export function deepMerge(...objs: any[]): any {
  const result = Object.create(null)
  
  // objs: [val1, val2]
  objs.forEach(obj => {
    if (obj) {
      Object.keys(obj).forEach(key => {
        const val = obj[key]
        if (isPlainObject(val)) {
          // header1和headers2中可能存在相同属性，不能覆盖掉
          if (isPlainObject(result[key])) {
            result[key] = deepMerge(result[key], val)
          } else {
            result[key] = deepMerge({}, val)
          }
        } else {
          result[key] = val
        }
      })
    }
  })

  return result
}